/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.Random;

/**
 *
 * @author Green
 */
public class Deck {

    public static BufferedImage cardsuittexture;
    public static BufferedImage backfacetexture;
    private static ArrayList<GUICard> lstSampleCards = new ArrayList<GUICard>();
    public ArrayList<GUICard> lstCards = new ArrayList<GUICard>();
    private static int width;
    private static int height;

    public static void setWidth(int w) {
        width = w;
    }

    public static void setHeight(int h) {
        height = h;
    }

    public Deck() {
        this.loadSampleCards();
        this.loadNewCards();
    }
//    public static loadResource() {
//        width = 71;
//        height = 96;
//        cardsuittexture = Toolkit.getDefaultToolkit().getImage((getClass().getResource("Images/StandardDeck.gif")));
//        backfacetexture = Toolkit.getDefaultToolkit().getImage((getClass().getResource("Images/backfacecard.png")));
//        Card.width = width;
//        Card.height = height;
//    }

    private void loadSampleCards() {
        lstSampleCards.clear();
        for (int i = 0; i < 4; i++) {
            ArrayList<GUICard> cardSuit = this.loadCardSuit(i);
            lstSampleCards.addAll(i * 13, cardSuit);
        }
    }

    public void loadNewCards() {
        this.reset();
    }

    private ArrayList<GUICard> loadCardSuit(int suit) {
        ArrayList<GUICard> cardSuit = new ArrayList<GUICard>();
        int yPos = suit;
        switch (suit) {
            case 0:
                yPos = 3;
                break;
            case 1:
                yPos = 0;
                break;
            case 2:
                yPos = 1;
                break;
            case 3:
                yPos = 2;
                break;
        }

        for (int i = 0; i < 13; i++) {

            int k = i;
            if (i == 0) {
                BufferedImage bImg = cardsuittexture.getSubimage(12 * (GUICard.width + 1), yPos * (GUICard.height + 1), GUICard.width, GUICard.height);
                GUICard card = new GUICard(suit, 12, bImg, backfacetexture);// rank = i + 1 nhé
                cardSuit.add(card);
            } else {
                k = i - 1;
                BufferedImage bImg = cardsuittexture.getSubimage(i * (GUICard.width + 1), yPos * (GUICard.height + 1), GUICard.width, GUICard.height);
                GUICard card = new GUICard(suit, k, bImg, backfacetexture);// rank = i + 1 nhé
                cardSuit.add(card);
            }
        }

        return cardSuit;
    }

    public void shuffle() {
        Calendar calendar = new GregorianCalendar();
        long seed = calendar.getTime().getSeconds();
        Random rand = new Random(seed);
        Collections.shuffle(lstCards, rand);
    }

    public void reset() {
        this.lstCards.clear();
        for (int i = 0; i < 52; i++) {
            GUICard card = lstSampleCards.get(i).clone();
            this.lstCards.add(card);
        }
    }

    public GUICard getCard(int rank, int suit) {
        for (int i = 0; i < 52; i++) {
            GUICard card = this.lstCards.get(i);
            if (card.rank == rank && card.suit == suit) {
                this.lstCards.remove(card);
                return card;
            }
        }

        return null;
    }

    public void drawBackFace(Graphics g, int x, int y) {
        g.drawImage(backfacetexture, x, y, null);
    }
}
